package wp.wf.com.myapplication.utils;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import wp.wf.com.myapplication.myinterface.CallBack;
import wp.wf.com.myapplication.myinterface.DataMode;

/**
 * Created by KF on 2016/4/25.
 */
public class DataModeImpl implements DataMode {

    @Override
    public void getDataPost(RequestParams params, final Class clazz, final CallBack callBack) {
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callBack.onSuccess(transformJavaBean(result, clazz));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                callBack.onError(ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                callBack.onFinish("finish");
            }
        });
    }

    @Override
    public void getDataGet(RequestParams params, Class clazz, CallBack callBack) {

    }

    public Object transformJavaBean(String info, Class clazz) {
        Gson gson = new Gson();
        return gson.fromJson(info, clazz);
    }

}
